package com.tj.jinzao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nim.uikit.business.session.emoji.MoonUtil;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseListAdapter;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Article;
import com.tj.jinzao.utils.Utils;
import com.tj.jinzao.widgets.RoundedImageView;

import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 我的日记 Adapter
 * Created by wjy on 2018/10/12.
 */
public class MyArticleListItemAdapter extends BaseListAdapter<Article, MyArticleListItemAdapter.ViewHolder> {

    private boolean mRevert = true;
    private int mAllNumber;

    public MyArticleListItemAdapter(Context context, CopyOnWriteArrayList data) {
        super(context, data);
    }

    public void setAllNumber(int number) {
        mAllNumber = number;
    }

    public void setRevert(boolean isRevert) {
        mRevert = isRevert;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_person_daily_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Article article = mData.get(position);
        String imageUrl = article.getImgurls();
        if (position == 0) {
            holder.year.setVisibility(View.VISIBLE);
            holder.year.setText(getYear(article.getReleaseTime()) + "年");
        } else {
            holder.year.setVisibility(View.GONE);
        }
        holder.lay_content.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(onLongClickLinstener!=null){
                    if(article.getUser().getId()== UserManager.getInstance().getUser().getId()){
                        onLongClickLinstener.onLongClick(article.getId());
                    }

                }
                return false;
            }
        });
        holder.title_date.setText(article.getReleaseTime());
        holder.number.setText("第" + (mRevert ? (mAllNumber - position) : (position + 1)) + "篇" + getArticleType(article));
        if (TextUtils.isEmpty(imageUrl)) {
            holder.imageLayout.setVisibility(View.GONE);
        } else {
            String images[] = imageUrl.split(",");
            holder.imageLayout.setVisibility(View.VISIBLE);
            if (images.length == 1 || article.isVideo()) {
                holder.image1.setVisibility(View.GONE);
                holder.image2.setVisibility(View.GONE);
                holder.image.setVisibility(View.VISIBLE);
                holder.play.setVisibility(article.isVideo() ? View.VISIBLE : View.GONE);
                if(images[0].contains("http")){
                    Glide.with(mContext).load(images[0]).into(holder.image);
                }else {
                    Glide.with(mContext).load(Constant.IMAGE_PREFIX + images[0]).into(holder.image);
                }
            } else {
                holder.image1.setVisibility(View.VISIBLE);
                holder.image2.setVisibility(View.VISIBLE);
                if(images[0].contains("http")){
                    Glide.with(mContext).load(images[0]).into(holder.image1);
                    Glide.with(mContext).load(images[1]).into(holder.image2);
                }else {
                    Glide.with(mContext).load(Constant.IMAGE_PREFIX + images[0]).into(holder.image1);
                    Glide.with(mContext).load(Constant.IMAGE_PREFIX + images[1]).into(holder.image2);
                }

                holder.image.setVisibility(View.GONE);
                holder.play.setVisibility(View.GONE);
            }
        }
        String tag = null;
        if(article!=null&&article.getTag()!=null&&article.getTag().contains(",")){
            String [] tags= article.getTag().split(",");
            for (int i=0;i<tags.length;i++ ){
                if(i==0){
                    tag=tags[i];
                }else {
                    tag=tag+","+tags[i];
                }

            }
            holder.label.setText("#"+tag);
        }else {
            holder.label.setText("");
        }
        holder.name.setText(article.getUser().getNickname());
        holder.vip.setText("V" + article.getUser().getLevel());
        RequestOptions options = new RequestOptions();
        options.placeholder(article.getUser().getGender() == 1
                ? R.mipmap.default_head_women : R.mipmap.default_head_man);
        Glide.with(mContext).load(Constant.IMAGE_PREFIX + article.getUser().getImgurl()).apply(options).into(holder.head);
        MoonUtil.identifyFaceExpression(NimUIKit.getContext(), holder.content
                , article.getContent()
                        .replace(Constant.ARTICLE_TAG, "")
                        .replace(Constant.ARTICLE_P1,"")
                        .replace(Constant.ARTICLE_P2,"")
                , ImageSpan.ALIGN_BOTTOM);
        holder.comments.setText(String.valueOf(article.getComment()));
        holder.date.setText(getDate(article.getReleaseTime()));
        holder.views.setText(String.valueOf(article.getView()) + "人已看");
        holder.praises.setText(String.valueOf(article.getLike()));
    }
    public interface OnLongClickLinstener{
        void onLongClick(Long id);
    }
    public OnLongClickLinstener onLongClickLinstener;
    public void setOnLongClickLinstener(OnLongClickLinstener onLongClickLinstener){
        this.onLongClickLinstener=onLongClickLinstener;
    }

    private String getArticleType(Article article) {
        if (article.isVideo()) return "视频";
        else if (article.getType() == 1) return "日记";
        else return "话题";
    }

    private String getYear(String time) {
        try {
            return time.substring(0, 4);
        } catch (Exception e) {
            return "2019";
        }
    }

    private String getDate(String time) {
        try {
            String date = time.split(" ")[0];
            String number[] = date.split("-");
            return number[1] + "月" + number[2] + "日";
        } catch (Exception e) {
            return "";
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, vip, follow, date, content, label, views, praises, comments, year, number, title_date;
        LinearLayout imageLayout;
        ImageView image1, image2, image, play;
        RoundedImageView head;
        RelativeLayout lay_content;

        public ViewHolder(View itemView) {
            super(itemView);
            year = itemView.findViewById(R.id.tv_year);
            title_date = itemView.findViewById(R.id.tv_date_title);
            number = itemView.findViewById(R.id.tv_number);
            name = itemView.findViewById(R.id.tv_name);
            vip = itemView.findViewById(R.id.tv_vip);
            follow = itemView.findViewById(R.id.tv_follow);
            date = itemView.findViewById(R.id.tv_date);
            content = itemView.findViewById(R.id.tv_content);
            label = itemView.findViewById(R.id.tv_label);
            views = itemView.findViewById(R.id.tv_view);
            praises = itemView.findViewById(R.id.tv_praise);
            comments = itemView.findViewById(R.id.tv_comment);
            head = itemView.findViewById(R.id.iv_head);
            imageLayout = itemView.findViewById(R.id.ll_image_layout);
            image1 = itemView.findViewById(R.id.iv_image_1);
            play = itemView.findViewById(R.id.iv_play);
            lay_content=itemView.findViewById(R.id.lay_content);
            int itemWidth = (Constant.SCREEN_WIDTH - Utils.dpToPx(74, mContext.getResources())) / 2;
            ViewGroup.LayoutParams layoutParams = image1.getLayoutParams();
            layoutParams.width = itemWidth;
            layoutParams.height = itemWidth;
            image1.setLayoutParams(layoutParams);
            image2 = itemView.findViewById(R.id.iv_image_2);
            image = itemView.findViewById(R.id.iv_image);
            ViewGroup.LayoutParams layoutParams1 = image2.getLayoutParams();
            layoutParams1.width = itemWidth;
            layoutParams1.height = itemWidth;
            image2.setLayoutParams(layoutParams1);
            ViewGroup.LayoutParams layoutParams2 = image.getLayoutParams();
            layoutParams2.width = Constant.SCREEN_WIDTH - Utils.dpToPx(64, mContext.getResources());
            layoutParams2.height = itemWidth;
            image.setLayoutParams(layoutParams2);
        }
    }


}
